#include <iostream>
#include<opencv2/opencv.hpp>

using namespace std;
using namespace cv;

#define COL 1024
#define ROW 1024



void printArray(long* a ,int n)
{

    for(int i=0;i<10;i++)
    {
        cout<<*(a+i)<<"  ";
    }
}

void printArray2(ushort* a ,int n)
{

    for(int i=0;i<10;i++)
    {
        cout<<*(a+i)<<"  ";
    }
}


int main()
{



    //读取图像
    Mat offset_high = imread("C:\\Users\\1米89叶超\\Desktop\\shuang2\\2022_12_12_14_21_20offset_high.png",0);
    Mat offset_low =  imread("C:\\Users\\1米89叶超\\Desktop\\shuang2\\2022_12_12_14_21_20offset_low.png",0);
    Mat gain_high =   imread("C:\\Users\\1米89叶超\\Desktop\\shuang2\\2022_12_12_14_21_23gain_high.png",0);
    Mat gain_low =    imread("C:\\Users\\1米89叶超\\Desktop\\shuang2\\2022_12_12_14_21_23gain_low.png",0);


     const int row = offset_high.rows;               //列(i,j)
     const int col = offset_high.cols;               //行

    //对图像进行

     long low_offset_average[COL]={0};
     long high_offset_average[COL]={0};
     long low_gain_average[COL]={0};
     long high_gain_average[COL]={0};



     //求列平均
     for(int i=0;i<col;i++)
     {
         for(int j=0;j<row;j++)
         {
             high_offset_average[i]+= offset_high.at<ushort>(i,j);
             low_offset_average[i]+=   offset_low.at<ushort>(i,j);
             low_gain_average[i]  += gain_low.at<ushort>(i,j);
             high_gain_average[i] += gain_high.at<ushort>(i,j);
         }


     }
     for(int i=0;i<COL;i++)
     {
         high_offset_average[i]/=COL;
         low_offset_average[i]/=COL;
         low_gain_average[i]/=COL;
         high_gain_average[i]/=COL;
     }


     //得到动态范围
     ushort low_range[COL]={0};
     ushort high_range[COL]={0};

     for(int i=0;i<COL;i++)
     {
         low_range[i] = low_gain_average[i]-high_gain_average[i];
         high_range[i] = low_offset_average[i] - high_offset_average[i];
     }



     //对采集到的数据进行处理
     Mat high_image = imread("C:\\Users\\1米89叶超\\Desktop\\shuang2\\2022_12_12_14_24_24current_high.png");
     Mat low_image = imread("C:\\Users\\1米89叶超\\Desktop\\shuang2\\2022_12_12_14_24_24current_low.png");

     //将处理的数据放在这个数组中
     ushort (*low_current_sub)[ROW]= new ushort[COL][ROW];

     ushort (*high_current_sub)[ROW] = new ushort[COL][ROW];




     //用当前数据减去offset的值
     for(int i=0;i<ROW;i++)
     {
         for(int j=0;j<COL;j++)
         {
             low_current_sub[i][j] = low_image.at<ushort>(i,j)-low_offset_average[j];
             high_current_sub[i][j] = high_image.at<ushort>(i,j)-high_offset_average[j];

         }
     }


    //对动态范围和本体值-动态范围的值 滤波

     cv::Mat M1(Size(1024,1024),CV_8UC1,low_current_sub);
     cv::Mat M2(Size(1024,1024),CV_8UC1,high_current_sub);

     //均值滤波
     blur(M1,M1,Size(7,7));
     blur(M2,M2,Size(7,7));


     float (*R)[COL] = new float[ROW][COL];

     for(int i=0;i<low_image.rows;i++)
     {
         for(int j=0;j<low_image.cols;j++)
         {
             if(high_gain_average[j]-high_image.at<ushort>(i,j)<700)
             {
                 R[i][j]=0;
             }
             else if (M2.at<ushort>(i,j)<150 || M1.at<ushort>(i,j)<150) {
                 R[i][j]=0.1;     //背景
             }

             else
             {
                R[i][j]=log(low_range[j]/M1.at<ushort>(i,j)) / log(high_range[j]/M2.at<ushort>(i,j));
             }
         }
     }

    //对R值进行滤波
    for(int i=0;i<ROW;i++)
    {
        for(int j=0;j<COL;j++)
        {

            if(R[i][j]!=0 && R[i][j]!=0.1)
            {
                cout<<"("<<i<<", "<<j<<")"<<"  "<<R[i][j]<<endl;
            }
        }


    }



//    cv::Mat M3(Size(1024,1024),CV_8UC1,R);
//    namedWindow("1",WINDOW_FREERATIO);
//    imshow("1",M3);

    waitKey(0);

     delete []low_current_sub;
     delete []high_current_sub;
     delete []R;

}












